package base;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import mapsObjects.FigureManager;
import mapsObjects.immovable.*;
import mapsObjects.movable.tank.*;
import output.I_Output;
import _img.I_Image;
import _maps.I_Map;

import commandProcess.I_CommandProcess;

public class Game{

	//Images block
	private I_Image _images;
	public static Image brickWall;
	public static Image concreteWall;
	public static Image bush;
	public static Image ice;
	public static Image water;
	public static Image chicken;
	public static Image myTankUp;
	public static Image myTankDown;
	public static Image myTankLeft;
	public static Image myTankRight;
	public static Image myBullet;
	public static Image enemyBullet;
	
	//Output
	private I_Output _output;
	
	//Map
	private I_Map _map;
	private int[] intArr;
	
	//FigureManager
	private FigureManager _figureManager;
	
	//Tank
	private ATank tank;
	
	//Timer
	private Timer t;

	//Constants
	public final static int DEFAULT_NUMBER_OF_BIG_ITEMS = 13;
	public final static int DEFAULT_NUMBER_OF_SMALL_ITEMS_IN_BIG = 16;
	public final static int DEFAULT_NUMBER_OF_ITEMS_IN_LINE = (int) Math.sqrt(DEFAULT_NUMBER_OF_SMALL_ITEMS_IN_BIG);
	public final static int DEFAULT_BIG_ELEMENT_SIZE = 64;
	public final static int DEFAULT_ELEMENT_SIZE = DEFAULT_BIG_ELEMENT_SIZE / DEFAULT_NUMBER_OF_ITEMS_IN_LINE;
	
	public Game(I_Image images, FigureManager figureManager, I_CommandProcess command, I_Output output, I_Map map) {
		_images = images;
		_figureManager = figureManager;
		_output = output;
		_map = map;
		mapCreate();
		
		tank = new MyTank(0, 0, Game.DEFAULT_BIG_ELEMENT_SIZE, Game.DEFAULT_BIG_ELEMENT_SIZE);
		
		_figureManager.addTank(tank);
		tank.setMaxBullets(3);
		tank.setSpeed(3);
		
		initImages();
		initTimer(35);
		start();
	}
	
	//init Images
	private void initImages()
	{
			brickWall = _images.read("BrickWall");
			concreteWall = _images.read("ConcreteWall");
			bush = _images.read("Bush");
			ice = _images.read("Ice");
			water = _images.read("Water");
			chicken = _images.read("Chicken");
			myTankUp = _images.read("MyTank_Up");
			myTankDown = _images.read("MyTank_Down");
			myTankLeft = _images.read("MyTank_Left");
			myTankRight = _images.read("MyTank_Right");
			myBullet = _images.read("myBullet");
			enemyBullet = _images.read("enemyBullet");
	}
	
	//init Timer
	private void initTimer(int ms)
	{
		t = new Timer(ms, new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				/*try
			       {
			       Thread.sleep(10);*/
					draw();
			      /* }
			      catch (InterruptedException ee)
			       {
			       ee.printStackTrace();
			       }*/
			}
		});
	}
	
	//create Map
	public void mapCreate()
	{
		intArr = _map.returnArr();
		int xPos = 0, yPos = 0, counter = 0, counter2 = 0;
		
		//if is "CHICKEN" - we make 1 big item with "CHICKEN" image, else ("Other Image") - make DEFAULT_NUMBER_OF_SMALL_ITEMS_IN_BIG items with "Other Image"
		for (int i = 0; i < intArr.length; i++)
		{		
			counter2 = 0;
			for (int k = 0; k < DEFAULT_NUMBER_OF_ITEMS_IN_LINE; k++)
				for (int j = 0; j < DEFAULT_NUMBER_OF_ITEMS_IN_LINE; j++)
				{
					switch (intArr[i])
					{
					case I_Map.MAP_BRICK_SYMBOL:
						_figureManager.addImmovableElement(counter, counter2, new BrickWall(xPos + DEFAULT_ELEMENT_SIZE * j, yPos + DEFAULT_ELEMENT_SIZE * k, DEFAULT_ELEMENT_SIZE, DEFAULT_ELEMENT_SIZE));
						break;
					case I_Map.MAP_CONCRETE_SYMBOL:
						_figureManager.addImmovableElement(counter, counter2, new ConcreteWall(xPos + DEFAULT_ELEMENT_SIZE * j, yPos + DEFAULT_ELEMENT_SIZE * k, DEFAULT_ELEMENT_SIZE, DEFAULT_ELEMENT_SIZE));
						break;
					case I_Map.MAP_BUSH_SYMBOL:
						_figureManager.addImmovableElement(counter, counter2, new Bush(xPos + DEFAULT_ELEMENT_SIZE * j, yPos + DEFAULT_ELEMENT_SIZE * k, DEFAULT_ELEMENT_SIZE, DEFAULT_ELEMENT_SIZE));
						break;
					case I_Map.MAP_ICE_SYMBOL:
						_figureManager.addImmovableElement(counter, counter2, new Ice(xPos + DEFAULT_ELEMENT_SIZE * j, yPos + DEFAULT_ELEMENT_SIZE * k, DEFAULT_ELEMENT_SIZE, DEFAULT_ELEMENT_SIZE));
						break;
					case I_Map.MAP_WATER_SYMBOL:
						_figureManager.addImmovableElement(counter, counter2, new Water(xPos + DEFAULT_ELEMENT_SIZE * j, yPos + DEFAULT_ELEMENT_SIZE * k, DEFAULT_ELEMENT_SIZE, DEFAULT_ELEMENT_SIZE));
						break;
					case I_Map.MAP_CHICKEN_SYMBOL:
						if (k + j == 0)
							_figureManager.addImmovableElement(counter, counter2, new Chicken(xPos, yPos, DEFAULT_BIG_ELEMENT_SIZE, DEFAULT_BIG_ELEMENT_SIZE));
						break;
					}
					counter2++;
				}
			
			if ((xPos += DEFAULT_BIG_ELEMENT_SIZE) == DEFAULT_NUMBER_OF_BIG_ITEMS * DEFAULT_BIG_ELEMENT_SIZE)
			{
				xPos = 0;
				yPos += DEFAULT_BIG_ELEMENT_SIZE;
			}
			
			counter++;
		}
	}
	
	private void draw()
	{
		_output.setBufferedImage(_figureManager.drawFigures());
		_output.repaint();
	}

	public void start()
	{
		t.start();
	}
}